var constant = require('../../core/common/constant');
orderController.$inject = ['$scope', 'orderService'];

function orderController($scope, orderService) {

    orderService.getOrders(constant.WaitingOrder,function(response){
        console.log('orders-response:'+response);
        if(!response.data.result){

        }else{
            console.log('find success:'+response.data.obj);
            $scope.waitOrders = response.data.obj;

        }
    });


    $scope.waitTab = function () {
        console.log('wait');
        $scope.waitOrders=[];
        orderService.getOrders(constant.WaitingOrder,function(response){
            console.log('orders-response:'+response);
            if(!response.data.result){
                alert("Request Fail");
            }else{
                console.log('find success:'+response.data.obj);
                $scope.waitOrders = response.data.obj;

            }
        });
    }
    $scope.receiveTab = function () {
        console.log('receive');
        $scope.receiveOrders=[];
        orderService.getOrders(constant.ReceiveOrder,function(response){
            console.log('orders-response:'+response);
            if(!response.data.result){
                alert("Request Fail");
            }else{
                console.log('find success:'+response.data.obj);
                $scope.receiveOrders = response.data.obj;

            }
        });

    }
    $scope.rejectTab = function () {
        console.log('reject');
        $scope.rejectOrders=[];
        orderService.getOrders(constant.RejectOrder,function(response){
            console.log('orders-response:'+response);
            if(!response.data.result){
                alert("Request Fail");
            }else{
                console.log('find success:'+response.data.obj);
                $scope.rejectOrders = response.data.obj;

            }
        });
    }
    
    $scope.yes = function (_id) {
        console.log('yes:'+_id);
        var waitModel=$scope.waitOrders ;
        var data = {
            _id:_id,
            status:constant.ReceiveOrder
        }
        orderService.updateOrders(data,function (response) {
            if(!response.data.result){
                alert("Request Fail");
            }else{
                alert("Request Suceess");

                $scope.waitOrders=waitModel.filter(function(item) {
                    return item._id != _id;
                });
            }
        });
    }
    $scope.no = function (_id) {
        console.log('yes:'+_id);
        var waitModel=$scope.waitOrders ;
        var data = {
            _id:_id,
            status:constant.RejectOrder
        }
        orderService.updateOrders(data,function (response) {
            if(!response.data.result){
                alert("Request Fail");
            }else{
                alert("Request Suceess");

                $scope.waitOrders=waitModel.filter(function(item) {
                    return item._id != _id;
                });
            }
        });
    }
    $scope.send = function (_id) {
        console.log('send:'+_id);

        var receiveModel=$scope.receiveOrders ;
        var data = {
            _id:_id,
            status:constant.SendingOrder
        }
        orderService.updateOrders(data,function (response) {
            if(!response.data.result){
                alert("Request Fail");
            }else{
                alert("Request Suceess");

                $scope.receiveOrders=receiveModel.filter(function(item) {
                    return item._id != _id;
                });
            }
        });
    }
}

module.exports = orderController;